<?php

namespace App\Factories;

use App\Models\User;
use Auth;
use Validator;
use Session;
use Msg;

class UserFactory extends Factory
{

    const salt = "WBC2016";
    /* 注册 */
    public function register($data,$autoPassword=false){
        $model = new User;
        if($autoPassword)
            $data['password'] = $this->genPassword();

        $msg = $this->registerValidata($data);
        if($msg)
            return $msg;
        $data['password'] =self::encrypt($data['password']);
        //return \App\Models\User::create($data);
        return $model->register($data);
    }

    /* 注册验证 */
    private  function registerValidata($data){
        $attrName = [
            'name'=>"用户名",
            'email' => "电子邮箱",
            'password'  => "密码",
            'rpassword'  => "重复密码",
        ];

        $res = $this->validate($data, [
            'name' => 'required|max:20',
            'email' => 'required|max:50',
            'password'  => 'required|max:20',
            'rpassword'  => 'required|max:20',
        ],$attrName);

        if(!$res['sta'])
            return $res;

        if($data['rpassword'] != $data['password'])
            return ['sta'=>0,'msg'=>'两次密码不一致'];

        $model = new User;
        $has = $model->where(['name' => $data['name']])->first();
        if($has){
            return ['sta'=>0,'msg'=>'用户名'.$data['name'].'已经存在'];
        }

        $has = $model->where(['email' => $data['email']])->first();
        if($has){

            return ['sta'=>0,'msg'=>'电子邮箱'.$data['email'].'已经存在'];
        }

    }
    /* 生成随机密码 */
    public function genPassword(){
        $PSW = new \App\Services\Password;
        return $PSW->createRandomPassword();
    }

    /* 重置密码 */
    public function resetPassword($pwd,$id){
        $model = new User;
        $pwd =self::encrypt($pwd);
        return $model->resetPassword($pwd,$id);
    }

    /* 重置密码验证 */
    private  function resetValidata($data){
        $attrName = [
            'password'  => "密码",
            'rpassword'  => "密码",
        ];

        $res = $this->validate($data, [
            'password'  => 'required|max:20',
            'rpassword'  => 'required|max:20',
        ],$attrName);

        if(!$res['sta'])
            return $res;

        if($data['rpassword'] != $data['password'])
            return ['sta'=>0,'msg'=>'两次密码不一致'];

    }

    /* 冻结用户 */
    public function forzenUser(){

    }


    /* 登录 */
    public function login($data){
        $data['password'] =self::encrypt($data['password']);
        $model = new User;
        $user_info = $model->login($data);

        if($user_info){
            Session::forget("user_info");
            Session::put(compact("user_info"));
            Session::save();

            return true;
        }else{
            return false;
        }
    }

    /* 登出 */
    public function logout(){
       Session::forget("user_info");
    }

    /* 获得当前账户 */
    public function getCurrent(){
        return Session::get("user_info");
    }

    /* 是否登录 */
    public function isLogin(){

    }
    /* 重置密码后邮件 */
    public function sendMail(){

    }

    /* 密码加密*/
    private static function encrypt($pwd){
        return md5(md5($pwd.self::salt));
    }

    /* 密码验证 */
    public function getUserInfo($data){
        $model = new User;
        $data['password'] =self::encrypt($data['password']);
        return $model->getUserInfo($data);
    }
}
